<?php
/**
 * WAVESELF FRAMEWORK
 * 
 * Waveself framework is an open source project released under New BSD license.
 * Waveself Engine Team did this in order to help you as ourselves with different
 * but still easy to use and learn approach. 
 * 
 * Please read license before you do any changes or to say, before using current software
 * read our licence which can be found under waveself framework root folder.
 * 
 * If you did not recieve copy of licence, contact us immediately and we will send you copy.
 * 
 * @category   Waveself
 * @package    System Configuration
 * @author     Nevio Vesic <nevio.vesic@gmail.com>
 * @copyright  Waveself Framework NPO 2009-2010
 * @license    https://code.google.com/p/waveself-framework/source/browse/trunk/LICENSE   New-BSD
 * @version    0.1a
 */

/**
 * ...
 * 
 * @category    Waveself
 * @package     Engine
 * @subpackage  Project
 * @author      Nevio Vesic <nevio.vesic@gmail.com>
 * @copyright   Waveself Framework NPO 2009-2010
 * @version     0.1 
 */
class Waveself_Engine_Project extends Waveself_Engine_Abstract
{
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	private $_router  = null;
	
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	private $_proj = null;
	
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	private $_isProjectUriBased = false;
	
	/** Project component constants **/
	CONST SETTINGS_PROJ_FILE    = 'settings/project.php';
	
	/**
	 * Project settings container.
	 *
	 * @var NULL|Waveself_Settings
	 */
	private $_psettings = null;
	
	/**
	 * Enter description here...
	 *
	 */
	public function __construct()
	{
		$wsettings = $this->getSettings('waveself')->getRecords();
		$this->_router = $this->getRouter();
		$route = $this->_router->getRouterData();
		
		/** Firstly what we need to find out is project...
            This method will attempt to allocate project and retreive 
			it's project settings file */
		$this->_loadProject();
		
		/** Secondly what we need to find out is current route... */

		/** Now we need to attempt load hooks */
		
		/** And than we need to load controller file */
		
		/** After all that we gotta prepare method */
	}
	
	/**
	 * Enter description here...
	 *
	 */
	private function _loadProject()
	{
		$route = $this->_router->getRouterData();
		$wsettings = $this->getSettings('waveself')->getRecords();
		
		foreach($wsettings->project as $project)
		{
			if($this->getRouter()->isSubdomain()) {
				$subdomain = $this->getRouter()->getSubdomain();
				if ($project['type'] == Waveself_Engine::PROJ_SUBDOMAIN) {
					if($project['accessKey'] == $subdomain) {
						$this->_proj = $project;
					}
				}
			}
			/** Case, subdomain is not registred... */
			else{
				
				/** Let's go search for NULL accessKey **/
				if (is_null($route)) {
					if($project['accessKey'] == NULL) {
						$this->_proj = $project;
					}
				}
				/** Case there is route */
				else {
					if($project['accessKey'] == self::$_routerData[0]) {
						$this->_isProjectUriBased = TRUE;
						$this->_proj = $project;
					}
				}
			}
			
			/** Case there is no _proj defined, let's go load default project! */
			if(!isset($this->_proj)) {
				$c = count(self::$_routerData);
				
				if($c == 4 && is_array(self::$_routerData[3])) {
					throw new Waveself_Exception('Illegal URL!');
				}
				
				if($project['default'] == TRUE) $this->_proj = $project;
			}
		}

		
		$sFile     = $this->_proj['path'] . self::SETTINGS_PROJ_FILE;
		$pInstance = $this->_proj['instance'];
		
		if (!isset(self::$_settings['project'][$pInstance])) {
			self::$_settings['project'][$pInstance] = new Waveself_Settings( $sFile, $this->_proj['instance'] );
			self::$_settings['project']['base']     = $this->_proj;
		}
		$this->_psettings = self::$_settings['project'][$pInstance];
		
	}
	
	/**
	 * Dispatch requested and resolved project!
	 * 
	 * @return  Void
	 */
	public function dispatch()
	{
		self::$_dispatcher = Waveself_Engine_Project_Dispatch::getInstance();
	}
	
	/**
	 * Return current project base url
	 *
	 * @return String[HTTP_HOST]
	 */
	public function getBaseUrl()
	{
		$wsettings = $this->getSettings('waveself')->getRecords();
		return $wsettings->engine->base->url;
	}
	
	/**
	 * Get Current Waveself Engine Version
	 *
	 * @return Integer|Float
	 */
	public function getVersion()
	{
		$wsettings = $this->getSettings('waveself')->getRecords();
		return $wsettings->engine->version;
	}
	
	/**
	 * Return Engine authors. There can be more than one author so
	 * please check if it's string or array first!
	 *
	 * @return String|Array
	 */
	public function getAuthors()
	{
		$wsettings = $this->getSettings('waveself')->getRecords();
		return $wsettings->engine->authors;
	}
	
	/**
	 * Get Waveself Engine Copyright
	 *
	 * @return String
	 */
	public function getCopyright()
	{
		$wsettings = $this->getSettings('waveself')->getRecords();
		return $wsettings->engine->copyright;
	}
	
	/**
	 * Get Waveself Engine update state
	 *
	 * @return Boolean
	 */
	public function updateState()
	{
		$wsettings = $this->getSettings('waveself')->getRecords();
		return $wsettings->engine->update->state;
	}
	
	/**
	 * Check whenever current system have access to update projects
	 * on the fly!
	 *
	 * @return Boolean
	 */
	public function updatesOnTheFly()
	{
		$wsettings = $this->getSettings('waveself')->getRecords();
		return $wsettings->engine->update->otf;
	}
}